import 'dart:async';
import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/pages/helpDoc/helpHome.dart';
import 'package:ichinesereader/themes/appTheme.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import '../../constants.dart';
import '../../pages/common/leaderBoard.dart';
import '../../pages/common/myBadges.dart';
import '../../pages/student/announcements.dart';
import '../../utils/helper.dart';

class FloatingActionButtonSideMenu extends StatefulWidget {
  FloatingActionButtonSideMenu({
    Key? key,
  }) : super(key: key);

  @override
  State<FloatingActionButtonSideMenu> createState() =>
      _FloatingActionButtonSideMenuState();
}

class _FloatingActionButtonSideMenuState
    extends State<FloatingActionButtonSideMenu> {
  final box = Hive.box('appconfig');

  Timer? _timer;

  @override
  void initState() {
    super.initState();

    EasyLoading.addStatusCallback((status) {
      if (status == EasyLoadingStatus.dismiss) {
        _timer?.cancel();
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    _timer?.cancel();
    EasyLoading.dismiss();
  }

  @override
  Widget build(BuildContext context) {
    return SpeedDial(
      animatedIcon: AnimatedIcons.menu_close,
      //openCloseDial: isDialOpen,
      backgroundColor: Get.theme.primaryColorDark,
      overlayColor: Colors.grey,
      overlayOpacity: 0.5,
      spacing: 5,
      spaceBetweenChildren: 5,
      closeManually: false,
      children: [
        SpeedDialChild(
            child: Icon(
              Icons.videogame_asset,
              size: 27.0,
              color: Get.isDarkMode ? Colors.black : AppTheme.colors.primary,
            ),
            label: 'Rewards'.tr,
            labelStyle: TextStyle(
                color: Get.isDarkMode
                    ? Get.theme.primaryColorLight
                    : Get.theme.primaryColorDark),
            labelBackgroundColor: Get.isDarkMode
                ? AppTheme.colors.darkTheme3
                : Get.theme.primaryColorLight,
            backgroundColor: Get.isDarkMode
                ? Get.theme.primaryColorDark
                : AppTheme.colors.textPrimaryDark,
            onTap: () {
              if (GetPlatform.isWeb || GetPlatform.isMacOS) {
                launchInWebViewWithJavaScript('${Constants.BaseURL}game/');
              } else {
                Navigator.pushNamed(context, '/student/reward');
              }
            }),
        SpeedDialChild(
            child: Icon(
              Icons.stars,
              size: 27.0,
              color: Get.isDarkMode ? Colors.black : AppTheme.colors.primary,
            ),
            label: 'My Badges'.tr,
            labelStyle: TextStyle(
                color: Get.isDarkMode
                    ? Get.theme.primaryColorLight
                    : Get.theme.primaryColorDark),
            labelBackgroundColor: Get.isDarkMode
                ? AppTheme.colors.darkTheme3
                : Get.theme.primaryColorLight,
            backgroundColor: Get.isDarkMode
                ? Get.theme.primaryColorDark
                : AppTheme.colors.textPrimaryDark,
            onTap: () {
              Get.to(() => MyBadgesPage());
            }),
        SpeedDialChild(
            child: Icon(
              Icons.leaderboard_rounded,
              size: 27.0,
              color: Get.isDarkMode ? Colors.black : AppTheme.colors.primary,
            ),
            label: 'Leader Board'.tr,
            labelStyle: TextStyle(
                color: Get.isDarkMode
                    ? Get.theme.primaryColorLight
                    : Get.theme.primaryColorDark),
            labelBackgroundColor: Get.isDarkMode
                ? AppTheme.colors.darkTheme3
                : Get.theme.primaryColorLight,
            backgroundColor: Get.isDarkMode
                ? Get.theme.primaryColorDark
                : AppTheme.colors.textPrimaryDark,
            onTap: () {
              Get.to(() => LeaderBoardPage());
            }),
        SpeedDialChild(
            child: Icon(
              Icons.help_outline_rounded,
              size: 27.0,
              color: Get.isDarkMode ? Colors.black : AppTheme.colors.primary,
            ),
            label: 'Help Center'.tr,
            labelStyle: TextStyle(
                color: Get.isDarkMode
                    ? Get.theme.primaryColorLight
                    : Get.theme.primaryColorDark),
            labelBackgroundColor: Get.isDarkMode
                ? AppTheme.colors.darkTheme3
                : Get.theme.primaryColorLight,
            backgroundColor: Get.isDarkMode
                ? Get.theme.primaryColorDark
                : AppTheme.colors.textPrimaryDark,
            onTap: () {
              Get.to(() => HelpDocsHome());
            }),
        SpeedDialChild(
            child: Icon(
              Icons.message_rounded,
              size: 27.0,
              color: Get.isDarkMode ? Colors.black : AppTheme.colors.primary,
            ),
            label: 'My Announcements'.tr,
            labelStyle: TextStyle(
                color: Get.isDarkMode
                    ? Get.theme.primaryColorLight
                    : Get.theme.primaryColorDark),
            labelBackgroundColor: Get.isDarkMode
                ? AppTheme.colors.darkTheme3
                : Get.theme.primaryColorLight,
            backgroundColor: Get.isDarkMode
                ? Get.theme.primaryColorDark
                : AppTheme.colors.textPrimaryDark,
            onTap: () {
              Get.to(() => StudentMyAnnouncements());
            }),
      ],
    );
  }
}
